//
// $Id: Notation.java 2 2007-04-17 19:39:30Z z_hossain $
//
// xChess is a multiplatform graphical chess user interface
// Copyright (C) 2007  MD. Z. Hossain <z_hossain@users.sf.net>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
// 02110-1301, USA.
//
// For more information visit http://xchess.sourceforge.net
//

package net.sourceforge.xchess;

import model.Move;
import gui.BoardUI;

public class Notation {

	public static final char[] FILE_CHAR = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h' };
	public static final char[] RANK_CHAR = { '1', '2', '3', '4', '5', '6', '7', '8' };

	public Notation() {
	}

	public int getFile(char f) {
		return f - 'a';
	}

	public int getRank(char r) {
		return r - '1';
	}

	public char getFile(int f) {
		return (char) ('a' + f);
	}

	public char getRank(int r) {
		return (char) ('1' + r);
	}
	
	public static String toString(int square) {
		return new String(new char[] {FILE_CHAR[square % BoardUI.FILE_RANK], 
						RANK_CHAR[(square / BoardUI.FILE_RANK)]});
	}
	
	public static int toSquare(String square) {
		return ((square.charAt(1) - '1')) * BoardUI.FILE_RANK + square.charAt(0) - 'a';
	}
	
	public static String toString(Move move) {
		return toString(move.getSource()) + toString(move.getDestination());
	}
	
	public static Move toMove(String notation) {
		return new Move(toSquare(notation.substring(0, 2)), 
				toSquare(notation.substring(2, 4)));
	}
}
